package com.lagou.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.*;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

/**
 * @Author wsl
 * @Date 2021/11/15 6:20
 * @Project_Name jdbc_spring
 */
@Configuration //声明为spring核心配置类
@ComponentScan("com.lagou")     //注解扫描包
@Import(DataSourceConfig.class) //导入其他配置类
@EnableTransactionManagement //事务的注解驱动
public class SpringConfig {

    @Bean //将该方法的返回值存到IOC容器中
    public JdbcTemplate getJdbcTemplate(@Autowired DataSource dataSource){

        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

        return jdbcTemplate;
    }

    @Bean //将该方法的返回值存到IOC容器中
    public PlatformTransactionManager getPlatformTransactionManager(@Autowired DataSource dataSource){

        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(dataSource);

        return dataSourceTransactionManager;
    }

}
